/*
 * Copyright (c) 2008 VMware, Inc.
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx;

import wtkx.collections.ArrayList;
import wtkx.collections.Sequence;
import wtkx.util.ImmutableIterator;
import wtkx.util.ListenerList;
import wtkx.wtk.content.MenuItemDataDecorator;

import java.util.Iterator;

/**
 * A {@link Menu} item.
 *
 * @author gbrown
 */
public class MenuItem 
    extends Button
{

    /**
     *
     */
    public class ItemListenerList
        extends ListenerList<Menu.ItemListener>
        implements Menu.ItemListener 
    {
        ItemListenerList(){
            super();
        }

        public void menuChanged(MenuItem item, Menu previousMenu) {
            for (Menu.ItemListener listener : this) {
                listener.menuChanged(item, previousMenu);
            }
        }
    }


    private static final Button.DataDecorator DEFAULT_DATA_RENDERER = new MenuItemDataDecorator();


    MenuSection section = null;
    Menu menu = null;

    private ItemListenerList itemListeners = new ItemListenerList();


    public MenuItem() {
        this(null);
    }
    public MenuItem(Object buttonData) {
        super(buttonData);

        setDataDecorator(DEFAULT_DATA_RENDERER);
        installSkin(MenuItem.class);
    }


    @Override
    public void setParent(Container parent) {
        if (parent != null
            && !(parent instanceof Menu)) {
            throw new IllegalArgumentException("Parent must be an instance of "
                                               + Menu.class.getName());
        }

        super.setParent(parent);
    }

    public MenuSection getSection() {
        return section;
    }

    public void setSection(MenuSection section) {
        this.section = section;
    }

    public Menu getMenu() {
        return menu;
    }

    public void setMenu(Menu menu) {
        if (menu != null
            && menu.getItem() != null) {
            throw new IllegalArgumentException("menu already belongs to an item.");
        }
        else {
            Menu previousMenu = this.menu;

            if (previousMenu != menu) {
                if (previousMenu != null) {
                    previousMenu.setItem(null);
                }

                if (menu != null) {
                    menu.setItem(this);
                }

                this.menu = menu;

                itemListeners.menuChanged(this, previousMenu);
            }
        }
    }

    @Override
    public void setTriState(boolean triState) {
        throw new UnsupportedOperationException("Menu items can't be tri-state.");
    }

    @Override
    public void press() {
        if (isToggleButton()) {
            setSelected(getGroup() == null ? !isSelected() : true);
        }

        super.press();

        if (menu == null) {
            MenuItem item = this;
            while (item != null
                   && item.section != null
                   && item.section.menu != null) {
                item.section.menu.getMenuItemSelectionListeners().itemSelected(this);
                item = item.section.menu.item;
            }
        }
    }

    public ListenerList<Menu.ItemListener> getItemListeners() {
        return itemListeners;
    }
}
